<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin;

use App\Service\SysConfigService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;

/**
 * 支付设置
 * Class PaySettingController.
 */
#[Controller(prefix: 'admin/pay-setting')]
class PaySettingController extends MainController
{
    #[Inject]
    protected SysConfigService $srv;

    #[GetMapping(path: 'list')]
    public function list()
    {
        $data = [
            [
                'id' => 1,
                'name' => '余额支付',
                'type' => SysConfigService::BALANCE,
                'img' => 'https://php-b2cplus-demo.likeshop.cn/resource/image/adminapi/pay/balance.png',
                'value' => $this->srv->getConfig(SysConfigService::BALANCE),
            ],
            [
                'id' => 2,
                'name' => '支付宝支付',
                'type' => SysConfigService::ALIPAY,
                'img' => 'https://php-b2cplus-demo.likeshop.cn/resource/image/adminapi/pay/alipay.png',
                'value' => $this->srv->getConfig(SysConfigService::ALIPAY),
            ],
            [
                'id' => 3,
                'name' => '微信支付',
                'type' => SysConfigService::WEPAY,
                'img' => 'https://php-b2cplus-demo.likeshop.cn/resource/image/adminapi/pay/wechat.png',
                'value' => $this->srv->getConfig(SysConfigService::WEPAY),
            ],
        ];
        return $this->success($data);
    }

    #[PostMapping(path: 'setRule')]
    public function setRule()
    {
        $key = $this->request->input('key');

        if ($key === SysConfigService::ALIPAY) {
            $this->srv->setConfig(SysConfigService::ALIPAY, [
                'appid' => $this->request->input('appid', ''),
                'private_key' => $this->request->input('private_key', ''),
                'ali_public_key' => $this->request->input('ali_public_key', ''),
            ]);
        } else {
            $this->srv->setConfig(SysConfigService::WEPAY, [
                // 微信
                'MCHID' => $this->request->input('MCHID', ''),
                'paySignKey' => $this->request->input('paySignKey', ''),
                'apiclient_cert' => $this->request->input('apiclient_cert', ''),
                'apiclient_key' => $this->request->input('apiclient_key', ''),
            ]);
        }

        return $this->success();
    }
}
